package web

import (
	"fmt"
	"jgt-oper/src/bll"
	"jgt-oper/src/context"
	"jgt-oper/src/schema"
	"jgt-oper/src/util"
	"time"
)

// SVController special-visit控制器
type SVController struct {
	SVBll *bll.SVisit `inject:""`
}

// QVisitPage 查询来访分页
func (a *SVController) QVisitPage(ctx *context.Context) error {
	ctx.WriteOperateLog("SVController.QVisitPage", "查询特殊来访列表")
	//	查询列表的页码和列表大小
	currentPage, pageSize := ctx.GetPageIndex(), ctx.GetPageSize()
	params := new(schema.SVQParams)
	params.PersonnelName = ctx.Query("personnel_name")
	params.ContractTel = ctx.Query("contract_tel")
	params.FromEnterprise = ctx.Query("from_enterprise")
	params.VisitingEnterprise = ctx.Query("visiting_enterprise")
	params.PersonnelTotal = ctx.QueryInt("personnel_total")
	params.MainContent = ctx.Query("main_content")
	params.StartTime = ctx.QueryInt64("start_time")
	params.EndTime = ctx.QueryInt64("end_time")
	total, items, err := a.SVBll.QVisitPage(params, currentPage, pageSize)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	itemsList := make([]map[string]interface{}, 0)
	for _, item := range items {
		tempMap := make(map[string]interface{})
		tempMap["record_id"] = item.RecordID
		tempMap["personnel_name"] = item.PersonnelName
		tempMap["contract_tel"] = item.ContractTel
		tempMap["from_enterprise"] = item.FromEnterprise
		tempMap["personnel_total"] = item.PersonnelTotal
		tempMap["visiting_enterprise"] = item.VisitingEnterprise
		tempMap["start_time"] = item.StartTime
		tempMap["end_time"] = item.EndTime
		tempMap["main_content"] = item.MainContent
		tempMap["status"] = "正常"
		if item.Status == 2 {
			tempMap["status"] = "作废"
		}
		tempMap["message_frequency"] = item.MessageFrequency
		itemsList = append(itemsList, tempMap)
	}
	result := map[string]interface{}{
		"list": itemsList,
		"pagination": map[string]interface{}{
			"total":    total,
			"current":  currentPage,
			"pageSize": pageSize,
		},
	}

	return ctx.ResSuccess(result)
}

// AddVisit 添加来访
func (a *SVController) AddVisit(ctx *context.Context) error {
	ctx.WriteOperateLog("SVController.AddVisit", "添加来访")
	//	获取创建数据
	item := new(schema.SVCParams)
	if err := ctx.ParseJSON(item); err != nil {
		return ctx.ResErrBadRequest(err)
	}
	//	添加来访
	itemBson := new(schema.SVBson)
	itemBson.ID = 0
	itemBson.RecordID = util.UUID()
	itemBson.Creator = ctx.GetUserID()
	itemBson.Created = time.Now().Unix()
	itemBson.PersonnelName = item.PersonnelName
	itemBson.ContractTel = item.ContractTel
	itemBson.FromEnterprise = item.FromEnterprise
	itemBson.VisitingEnterprise = item.VisitingEnterprise
	itemBson.PersonnelTotal = item.PersonnelTotal
	itemBson.MainContent = item.MainContent
	itemBson.StartTime = item.StartTime
	itemBson.EndTime = item.EndTime
	itemBson.ParkNodes = item.ParkNodes
	itemBson.FreezeEntrances = item.FreezeEntrances
	itemBson.MessageFrequency = item.MessageFrequency
	itemBson.Deleted = 0
	itemBson.Status = 1
	err := a.SVBll.AddVisit(itemBson)
	fmt.Println("AddVisit:", err)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResOK()
}

// QVisitDetail 查询来访详情
func (a *SVController) QVisitDetail(ctx *context.Context) error {
	ctx.WriteOperateLog("SVController.QVisitDetail", "特殊来访管理-查询来访详情")
	item, err := a.SVBll.QVisitDetail(ctx.Param("id"))
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	//	构造返回数据
	result := make(map[string]interface{})
	result["record_id"] = item.RecordID
	result["personnel_name"] = item.PersonnelName
	result["contract_tel"] = item.ContractTel
	result["from_enterprise"] = item.FromEnterprise
	result["visiting_enterprise"] = item.VisitingEnterprise
	result["personnel_total"] = item.PersonnelTotal
	result["main_content"] = item.MainContent
	result["start_time"] = item.StartTime
	result["end_time"] = item.EndTime
	result["park_nodes"] = item.ParkNodes
	result["freeze_entrances"] = item.FreezeEntrances
	return ctx.ResSuccess(result)
}

// Cancel 作废来访记录
func (a *SVController) Cancel(ctx *context.Context) error {
	ctx.WriteOperateLog("SVController.Cancel", "特殊来访管理-作废来访")
	err := a.SVBll.Cancel(ctx.Param("id"))
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResOK()
}

// Resend 重新发送短信
func (a *SVController) Resend(ctx *context.Context) error {
	ctx.WriteOperateLog("SVController.Resend", "特殊来访管理-重新发送短信")

	//	更新数据库记录
	err := a.SVBll.Resend(ctx.Param("id"))
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResOK()
}
